from flask_restplus import Resource, Namespace, fields, marshal, reqparse
from common.decorators import catch_error
from common.responses import ok
from common.response_model import base_model, pagination_model
from common.request_parser import get_parser as base_get_parser
from app.user.decorators import token_required
from app.finance.account_flow.model import AccountFlow
from app.finance.account_flow import dao

api = Namespace('account_flow')

# ---------- model ----------
account_flow_model = base_model.inherit('account_flow_model', {
    "transaction_type": fields.Integer(description='交易类型'),
    "account_id": fields.Integer(description='account_id'),
    "customer_id": fields.Integer(description='customer_id'),
    'order_id': fields.Integer(description='order_id'),

})
account_flow_paginate_model = pagination_model.inherit('account_flow_paginate_model', {
    "items": fields.Nested(account_flow_model)
})

# ---------- create parser ----------

# ---------- get parser ----------
get_parser = base_get_parser.copy()


# ---------- put parser ----------


class AccountFlowsResource(Resource):
    @catch_error
    @token_required
    @api.expect(get_parser)
    def get(self):
        """获取列表"""
        args = get_parser.parse_args()
        pagination = dao.get_by_page(**args)
        return ok(data=marshal(pagination, account_flow_paginate_model),
                  message='get success')


class AccountFlowResource(Resource):
    @catch_error
    @token_required
    def get(self, obj_id):
        """获取"""
        obj = AccountFlow.query.filter_by(id=obj_id).first()
        return ok(data=marshal(obj, account_flow_model),
                  message='get success')


# --------- route ----------
api.add_resource(AccountFlowsResource, '/account_flows')
api.add_resource(AccountFlowResource, '/account_flow/<int:obj_id>')

# 注册 response 文档
api.models[account_flow_model.name] = account_flow_model
